package com.kfit.config.shiro;

import java.util.HashMap;
import java.util.Map;

import org.beetl.core.resource.ClasspathResourceLoader;
import org.beetl.ext.spring.BeetlGroupUtilConfiguration;
import org.beetl.ext.spring.BeetlSpringViewResolver;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternUtils;
import org.springframework.web.servlet.ViewResolver;

import com.ibeetl.bbs.common.Const;
import com.kfit.ibeetl.bbs.common.Functions;

/**
 * beetl 配置
 * @author 草原狼
 * @date 2017-6-23
 */
@Configuration
public class BeetlConfiguration extends SpringBootServletInitializer {
	@Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(BeetlConfiguration.class);
    }
	
	@Bean(initMethod = "init", name = "beetlConfig")
    public BeetlGroupUtilConfiguration getBeetlGroupUtilConfiguration(@Qualifier("functions") Functions fn /*, final BbsModuleDao moduleDao*/) {

        BeetlGroupUtilConfiguration beetlGroupUtilConfiguration = new BeetlGroupUtilConfiguration();
        ResourcePatternResolver patternResolver = ResourcePatternUtils.getResourcePatternResolver(new DefaultResourceLoader());

        try {
            ClasspathResourceLoader cploder = new ClasspathResourceLoader(BeetlConfiguration.class.getClassLoader(),"templates/");
            beetlGroupUtilConfiguration.setResourceLoader(cploder);

            beetlGroupUtilConfiguration.setConfigFileResource(patternResolver.getResource("classpath:beetl.properties"));
            Map<String, Object> functionPackages = new HashMap<String, Object>();
            functionPackages.put("c", fn);
            beetlGroupUtilConfiguration.setFunctionPackages(functionPackages);
            beetlGroupUtilConfiguration.setSharedVars(new HashMap<String, Object>() {            	
				private static final long serialVersionUID = 7167033020193046364L;

			{
                put("v", Const.TIMESTAMP);
                //put("moduleList", moduleDao.all());
            }});
            
            return beetlGroupUtilConfiguration;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

    @Bean(name = "beetlViewResolver")
    public ViewResolver getBeetlSpringViewResolver(@Qualifier("beetlConfig") BeetlGroupUtilConfiguration beetlGroupUtilConfiguration) {
        BeetlSpringViewResolver beetlSpringViewResolver = new BeetlSpringViewResolver();
        beetlSpringViewResolver.setContentType("text/html;charset=UTF-8");
        beetlSpringViewResolver.setOrder(0);
        beetlSpringViewResolver.setConfig(beetlGroupUtilConfiguration);
        
       // InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        //beetlSpringViewResolver.setPrefix("templates/");
        //beetlSpringViewResolver.setSuffix(".html");
        //beetlSpringViewResolver.setExposeContextBeansAsAttributes(true);
        
        return beetlSpringViewResolver;
    }
    
   
}
